package com.be.beadmin.service.controller;

import com.alibaba.excel.EasyExcel;
import com.be.beadmin.common.entity.PageEntity;
import com.be.beadmin.common.utils.ResultModel;
import com.be.beadmin.service.dto.input.PrintDesignFileDto;
import com.be.beadmin.service.dto.output.PrintDesignFileEx;
import com.be.beadmin.service.service.PrintDesignFileService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.domain.Page;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RestController;

import javax.servlet.http.HttpServletResponse;
import java.io.IOException;
import java.net.URLEncoder;
import java.util.List;

/**
* @author chenwei
* @date 2023-05-10
**/
@RestController
public class PrintDesignFileController {

    @Autowired
    private  PrintDesignFileService printDesignFileService;


    @PostMapping(value = "/service/getPrintDesignFileByPage")
    public ResultModel getPrintDesignFileByPage(@RequestBody PageEntity pageEntity)  {
        return ResultModel.success(printDesignFileService.getPrintDesignFileByPage(pageEntity)) ;
    }

    @PostMapping("/service/addPrintDesignFile")
    public ResultModel addPrintDesignFile(@RequestBody PrintDesignFileDto printDesignFileDto){
        printDesignFileService.addPrintDesignFile(printDesignFileDto);
        return ResultModel.success(ResultModel.ADD);
    }

    @PostMapping("/service/delPrintDesignFile")
    public ResultModel delPrintDesignFile(@RequestBody PrintDesignFileDto printDesignFileDto){
        printDesignFileService.delPrintDesignFile(printDesignFileDto.getId());
        return ResultModel.success(ResultModel.DELETE);
    }


    @PostMapping("/service/updatePrintDesignFile")
    public ResultModel updatePrintDesignFile(@RequestBody PrintDesignFileDto printDesignFileDto){
        printDesignFileService.updatePrintDesignFile(printDesignFileDto);
        return ResultModel.success(ResultModel.UPDATE);
    }


    @PostMapping("/service/updateFileContent")
    public ResultModel updateFileContent(@RequestBody PrintDesignFileDto printDesignFileDto){
        printDesignFileService.updateFileContent(printDesignFileDto);
        return ResultModel.success(ResultModel.UPDATE);
    }

    @PostMapping("/service/exportPrintDesignFile")
    public void exportPrintDesignFile(HttpServletResponse response ,@RequestBody PageEntity pageEntity) throws IOException {
        Page<PrintDesignFileEx> printDesignFilePage = printDesignFileService.getPrintDesignFileByPage(pageEntity);
        List<PrintDesignFileEx> printDesignFileExes = printDesignFilePage.getContent();
        response.setContentType("application/vnd.openxmlformats-officedocument.spreadsheetml.sheet");
        response.setCharacterEncoding("utf-8");
        String fileName = URLEncoder.encode("PrintDesignFile接口", "UTF-8").replaceAll("\\+", "%20");
        response.setHeader("Content-disposition", "attachment;filename*=utf-8''" + fileName + ".xlsx");
        EasyExcel.write(response.getOutputStream(), PrintDesignFileEx.class).sheet("sheet1").doWrite(printDesignFileExes);
    }
}